﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Xml.Linq;
using System.Diagnostics;
using System.Windows.Media.Imaging;

namespace MySilverlightAlbum.Data
{
    public class ImageCollection : ObservableCollection<ImageFolder>
    {
        public ImageFolder Find(
            string                                      id
        )
        {
            foreach (ImageFolder f in Items)
            {
                if (f.Id == id)
                {
                    return f;
                }
            }
            return null;
        }


    }

    public class ImageFolder
    {
        private BitmapImage                             m_thumbnailImg;
        private BitmapImage                             m_img;

        public static ImageFolder FromXElement(
            XElement                                    element
        )
        {
            ImageFolder                                 n;
            ImageFolder                                 newFolder;    
            XElement                                    e;

            Debug.Assert(element != null);

            n = new ImageFolder();

            n.Id = Guid.NewGuid().ToString();
            n.Title = GetAttrValue(element, "title");
            n.MailTo = GetAttrValue(element, "mailto");
            n.Description = GetAttrValue(element, "description");
            n.Location = GetAttrValue(element, "location");
            n.Thumbnail = GetAttrValue(element, "thumbnail");

            foreach (XNode x in element.Nodes())
            {
                e = x as XElement;

                if (e != null && e.Name == "item")
                {
                    newFolder = ImageFolder.FromXElement(e);
                    newFolder.Parent = n;

                    n.Children.Add(newFolder);
                }
            }

            return n;
        }

        private static string GetAttrValue(
            XElement                                    e,
            string                                      name
        )
        {
            XAttribute                                  a;
            a = e.Attribute(name);
            
            return a == null ? String.Empty : a.Value;
        }

        public ImageFolder(
        )
        {
            Children = new ImageCollection();
        }

        public BitmapImage ThumbnailImage
        {
            get
            {
                if (m_thumbnailImg == null && !String.IsNullOrEmpty(Thumbnail))
                {
                    m_thumbnailImg = new BitmapImage(new Uri(Thumbnail, UriKind.RelativeOrAbsolute));
                }
                return m_thumbnailImg;
            }
        }

        public BitmapImage Image
        {
            get
            {
                if (m_img == null && !String.IsNullOrEmpty(Location))
                {
                    m_img = new BitmapImage(new Uri(Location, UriKind.RelativeOrAbsolute));
                }
                return m_img;
            }
        }

        public string Id
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }

        public string MailTo
        {
            get;
            set;
        }

        public string Location
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public string Thumbnail
        {
            get;
            set;
        }

        public ImageFolder Parent
        {
            get;
            set;
        }

        public bool IsFolder
        {
            get
            {
                return Children.Count > 0;
            }
        }

        public ImageFolder Find(
            string                                      id
        )
        {
            return Children.Find(id);
        }

        public ImageCollection Children
        {
            get;
            set;
        }
    }
}
